# Who Will Defend Democracy? Evaluating Tradeoffs in Candidate Support Among Partisan Donors and Voters
# Step 2: Read data (public sample)
# Last updated: June 19, 2020

# Initial settings --------------------------------------------------------

library(tidyverse)
library(readxl) 
library(haven)

# Read data ---------------------------------------------------------------

survey <- read_dta("data/MICH0038_B_output.dta") %>% 
  filter(consent == 1) %>% # consent
  filter(inputstate != 99) %>% # outside US
  filter(!(take_serious == 4 | take_serious == 5)) %>% # remove insincere responses 
  mutate(cand1_race = as_factor(cand1_race) %>% as.character())

survey <- as.data.frame(survey) 

# Respondent-level variables ----------------------------------------------

respondents <- survey %>% 
  mutate(num_senators = ifelse(num_senators == 2, 1, 0),
         prime_minister = ifelse(prime_minister == 4, 1, 0),
         senator_term = ifelse(senator_term == 3, 1, 0),
         prestimes = ifelse(prestimes == 2, 1, 0),
         houserep_term = ifelse(houserep_term == 1, 1, 0)) %>% 
  mutate(knowledge = num_senators + prime_minister + senator_term + prestimes + houserep_term) %>% 
  mutate(knowledge_dum = ifelse(knowledge < 3, 1, 2)) %>% # 1 = low, 2 = high
  mutate(education_dum = ifelse(educ7 < 5, 1, 2)) %>%  # 1 = low, 2 = high
  mutate(interest_dum = ifelse(polinterest > 2, 1, 2)) %>% # 1 = low, 2 = high
  mutate(age_dum = ifelse(age < median(age), 1, 2)) %>% # 1 = young, 2 = old
  mutate(trump_dum = ifelse(approve_trmp < 3, 1, 2)) %>% # 1 = pro, 2 = anti
  mutate(partisanship_dum_3 = NA,
         partisanship_dum_3 = ifelse(pid3 == "1" & !is.na(pid3), 2, partisanship_dum_3), # democrat = 2
         partisanship_dum_3 = ifelse(pid3 == "2" & !is.na(pid3), 1, partisanship_dum_3) # republican = 1
  ) %>% 
  mutate(partisanship_dum_7 = NA,
         partisanship_dum_7 = ifelse(pid7 == 1 & !is.na(pid7), 2, partisanship_dum_7), # strong democrat
         partisanship_dum_7 = ifelse(pid7 == 2 & !is.na(pid7), 2, partisanship_dum_7), # not very strong democrat
         partisanship_dum_7 = ifelse(pid7 == 3 & !is.na(pid7), 2, partisanship_dum_7), # democratic leaner
         partisanship_dum_7 = ifelse(pid7 == 5 & !is.na(pid7), 1, partisanship_dum_7), # republican leaner
         partisanship_dum_7 = ifelse(pid7 == 6 & !is.na(pid7), 1, partisanship_dum_7), # not very strong republican
         partisanship_dum_7 = ifelse(pid7 == 7 & !is.na(pid7), 1, partisanship_dum_7), # strong republican
         partisanship_dum_7 = ifelse(pid7 == 4 & !is.na(pid7), 3, partisanship_dum_7) # independent
  ) %>% 
  select(-contains("cand")) %>% 
  select(-contains("conjoint")) %>% 
  select(-contains("_t")) %>% 
  select(-contains("race_")) %>% 
  select(-consent, -birthyr, -age, -inputstate, -agreement, -prestimes,
         -num_senators, -prime_minister, -answer_lookup, -take_serious, -marstat,
         -child18, -employ, -votereg, -newsint, -pew_bornagain, -pew_religimp, -pew_churatd,
         -pew_prayer, -religpew, -religpew_protestant) %>%
  mutate_if(is.labelled, as.character)

save.image("temp/data_for_summary_stats_public.Rdata")

# Make a function ---------------------------------------------------------

custom_reshape <- function(attribute){
  
  att <- enquo(attribute)
  att_name <- quo_name(att)
  
  out <- survey %>%
    select(caseid, contains(paste0("_", !!att_name))) %>% 
    mutate(caseid = as.numeric(caseid)) %>% 
    mutate_at(.vars = vars(2:21), 
              .funs = funs(as_factor(.) %>% as.character())) %>% 
    gather("candidate", !!att_name, 2:ncol(.)) %>% 
    mutate(candidate_id = str_extract(candidate, "\\d+") %>% as.integer()) %>% 
    mutate(profile = ifelse(candidate_id %in% seq(from = 1, to = 20, by = 2), 1, 2),
           task = ceiling(candidate_id/2)) %>%
    select(caseid, task, profile, !!att_name) %>%
    arrange(caseid, task, profile)
  
}

att1 <- custom_reshape(name)
att2 <- custom_reshape(party)
att3 <- custom_reshape(elect)
att4 <- custom_reshape(invest)
att5 <- custom_reshape(comp)
att6 <- custom_reshape(courts)
att7 <- custom_reshape(discrim)
att8 <- custom_reshape(govt)

attribute_levels <- att1 %>%
  full_join(att2) %>% 
  full_join(att3) %>% 
  full_join(att4) %>% 
  full_join(att5) %>% 
  full_join(att6) %>% 
  full_join(att7) %>% 
  full_join(att8) 

attribute_levels <- attribute_levels %>% 
  mutate(name = str_to_lower(name) %>% str_trim())

demographics <- read_xlsx("data/candidate_names.xlsx") %>% 
  mutate(Name = str_to_lower(Name) %>% str_trim()) %>% 
  rename(name = `Name`) %>% 
  mutate(Gender = if_else(str_detect(Group, "M"), "Male", "Female"),
         Race = "White",
         Race = if_else(str_detect(Group, "B"), "Black", Race),
         Race = if_else(str_detect(Group, "L"), "Hispanic", Race))

attribute_levels <- attribute_levels %>% 
  left_join(demographics, by = "name") %>% 
  select(caseid, task, profile, party, Gender, Race, everything(), -Group, -name) %>% 
  rename(Partisanship = `party`)

# Outcomes ----------------------------------------------------------------

outcomes <- paste0("conjoint", 1:10) 

responses <- survey %>%
  select("caseid", outcomes) %>% 
  gather("task", "value", 2:ncol(.)) %>% 
  mutate(task = str_replace_all(task, "conjoint", "") %>% 
           as.numeric() %>% 
           as.factor() %>% 
           as.numeric() 
  ) 

# Merge and wrangle data ---------------------------------------------------

df_public <- attribute_levels %>% 
  left_join(responses) %>% 
  left_join(respondents) %>%
  
  # Recode outcome variables
  mutate(selected = ifelse(profile == value, 1, 0)) %>% 
  select(-value) %>%
  
  # Select and sort variables
  select(caseid, starttime, endtime, weight,
         task, profile, selected, 
         everything()) %>% 
  
  # Rename attributes and levels
  rename(`Voting` = `elect`,
         `Compromise` = `comp`,
         `Investigations` = `invest`,
         `Courts` = `courts`,
         `Discrimination` = `discrim`,
         `Taxes` = `govt`,
         `id` = `caseid`) %>%
  mutate(`Voting` = 
           gsub("Supports new legislation to require voters to show state-issued ID at the polls.",
                "Supports voter ID laws",
                `Voting`)) %>% 
  mutate(`Voting` = 
           gsub("Opposes new legislation to require voters to show state-issued ID at the polls.",
                "Opposes voter ID laws",
                `Voting`)) %>% 
  mutate(`Compromise` = 
           gsub("Promises to stand up to the other party.",
                "Stand up to other party",
                `Compromise`)) %>% 
  mutate(`Compromise` = 
           gsub("Promises to work for compromise across party lines.",
                "Supports compromise",
                `Compromise`)) %>% 
  mutate(`Investigations` = 
           gsub("Said elected officials should supervise law enforcement investigations of politicians and their associates.",
                "Partisan involvement",
                `Investigations`)) %>% 
  mutate(`Investigations` = 
           gsub("Said law enforcement investigations of politicians and their associates should be free of partisan influence.",
                "Independent",
                `Investigations`)) %>%
  mutate(`Courts` = 
           gsub("Said elected officials should not be bound by court decisions they regard as politicized.",
                "Disregard politicized decisions",
                `Courts`)) %>% 
  mutate(`Courts` = 
           gsub("Said elected officials must obey the courts even when they think that the decisions are wrong.",
                "Obey courts",
                `Courts`)) %>% 
  mutate(`Discrimination` = 
           gsub("Believes discrimination against racial minorities is less of a problem now than in the past.",
                "Not a big problem",
                `Discrimination`)) %>% 
  mutate(`Discrimination` = 
           gsub("Believes the government should do more to prevent discrimination against racial minorities.",
                "Prevent discrimination",
                `Discrimination`)) %>% 
  mutate(`Taxes` = 
           gsub("Wants to lower taxes on everyone, including the wealthy.",
                "Less progressive",
                `Taxes`)) %>% 
  mutate(`Taxes` = 
           gsub("Wants to raise taxes on the wealthy.",
                "More progressive",
                `Taxes`)) %>% 
  
  # Change attributes to factors
  mutate_at(.vars = c("Partisanship", 
                      "Gender",
                      "Race",
                      "Compromise", 
                      "Discrimination",
                      "Voting",
                      "Investigations",
                      "Courts",
                      "Taxes"), funs(as.factor(.)))

# Save data.frame  --------------------------------------------------------

# Save data 

df_cleaned_public <- df_public %>%
  filter(!is.na(selected))

# Remove unncessary objects
rm(list= ls()[!(ls() %in% c("df_cleaned_public"))])

save.image("temp/data_for_analysis_public.RData")
